package com.minggo.charmword.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.baidu.mobstat.StatService;
import com.minggo.charmword.R;
import com.minggo.charmword.adapter.LockHistoryAdapter;
import com.minggo.charmword.model.ReviewHistory;
import com.minggo.charmword.service.CharmWordApplication;

/**
 * 使用历史
 * @author minggo
 * @time 2014-10-1下午11:39:01
 */
public class UseHistoryActivity extends Activity implements OnItemClickListener {

	private List<ReviewHistory> historyList = new ArrayList<ReviewHistory>();
	private LockHistoryAdapter lockHistoryAdapter;
	private ListView historyLv;
	private View backV;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		initUI();
		requestData();
		CharmWordApplication.allActivities.add(this);
	}
	
	private void initUI(){
		backV = findViewById(R.id.lo_use_history_back);
		historyLv = (ListView)findViewById(R.id.lv_history);
		
		backV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});
	}
	/**
	 * 请求数据
	 */
	private void requestData() {
		historyList.clear();
		for (int i = 0; i < 10; i++) {
			ReviewHistory reviewHistory = new ReviewHistory();
			reviewHistory.contiueDays = 10 + i;

			if (i == 0) {
				reviewHistory.dateTime = new Date().getTime();
				reviewHistory.lockCount = 1;
			} else {
				reviewHistory.dateTime = new Date(new Date().getTime() - (i * (24 * 60 * 60 * 1000))).getTime();
				reviewHistory.lockCount = 20 + i;
			}
			reviewHistory.historyId = i + i;
			reviewHistory.isReview = 1;

			historyList.add(reviewHistory);
		}
		handler.sendEmptyMessage(1001);
	}

	/**
	 * 刷新数据
	 */
	public void refreshUI() {
		if (lockHistoryAdapter == null) {
			lockHistoryAdapter = new LockHistoryAdapter(this, historyList);
			historyLv.setAdapter(lockHistoryAdapter);
			// lockHistoryAdapter.notifyDataSetChanged();
			
			AnimationSet set = new AnimationSet(false);
			Animation animation = new AlphaAnimation(0, 1); // AlphaAnimation
															// 控制渐变透明的动画效果
			animation.setDuration(300); // 动画时间毫秒数
			set.addAnimation(animation);
			LayoutAnimationController controller = new LayoutAnimationController(set, 1);
			historyLv.setLayoutAnimation(controller);
			
		} else {
			lockHistoryAdapter.notifyDataSetChanged();
		}
	}

	private Handler handler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case 1001:
				refreshUI();
				break;

			default:
				break;
			}
			return false;
		}
	});
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == 0) {// 调用分享

		}
	}
	@Override
	protected void onPause() {
		super.onPause();
		StatService.onPause(this);
	}
	@Override
	protected void onResume() {
		super.onResume();
		StatService.onResume(this);
	}
	
}
